package com.ensah.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.boudaa.dao.exceptions.EntityNotFoundException;
import com.ensah.dao.AbsenceDao;
import com.ensah.dao.EtudiantDao;
import com.ensah.metier.bo.Absence;
import com.ensah.metier.bo.Etudiant;
import com.ensah.metier.bo.Matiere;
import com.ensah.service.AbsenceService;
import com.ensah.service.EtudiantService;
import com.ensah.service.MatiereService;

public class AbsenceServiceImpl implements AbsenceService{

   private AbsenceDao absDao;
   private EtudiantDao etdao;
	public void delete(Long idAbs) {
		
		absDao.delete(idAbs);
	}


	public Absence findById(Long pId) throws EntityNotFoundException {
	
		return absDao.findById(pId);
	}

	
	public List<Absence> getAll() {
		return absDao.getAll();
	}

	
	public Absence save(Absence abs) {
		return absDao.create(abs);
	}

	
	public void update(Absence pObject) {
		absDao.update(pObject);
		
	}
	List<Absence> getAbsenceByCne(String cne) throws EntityNotFoundException{
		Etudiant etd=etdao.findEtudiantByCne(cne);
		
		return absDao.getAbsenceByIdEtd(etd.getIdUtil());
		
	}

	public AbsenceDao getAbsDao() {
		return absDao;
	}


	public void setAbsDao(AbsenceDao absDao) {
		this.absDao = absDao;
	}
	public static void main(String[] args) throws EntityNotFoundException {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        AbsenceService absServ= (AbsenceService) ctx.getBean("idserviceAbs")	;
        MatiereService matServ=(MatiereService) ctx.getBean("idserviceMat");
        EtudiantService etdServ=(EtudiantService) ctx.getBean("idserviceEtd");
		Matiere matiere=matServ.findById(Long.valueOf(1));
		Absence abs=new Absence(new Date(), "s1", false, matiere);
		abs=absServ.save(abs);
		
		Etudiant etd=etdServ.getEtudiantById(Long.valueOf(1));
		List<Absence> absences=new ArrayList<Absence>();
		absences.add(abs);
		etd.setAbsences(absences);
		etdServ.modifierEtudiant(etd);
	}

}
